package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.config.R;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.entity.DishDto;
import com.example.service.DishFloverService;
import com.example.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("dish")
public class DishFlavorController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFloverService dishFloverService;

    @RequestMapping("/page")
    public R page(Integer page, Integer pageSize, String name){
        IPage<Dish> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        if (name!=null){
            queryWrapper.like(Dish::getName, name);
        }
        //queryWrapper.orderByDesc(Employee::getUpdateTime);
        //loginService.page(pageInfo,queryWrapper);
        IPage<Dish> iPage = dishService.page(pageInfo, queryWrapper);
        return R.success(iPage);
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody DishDto dishDto){
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        //添加创建人和修改人
        Long empId = (Long) request.getSession().getAttribute("employee");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        dishService.saveWithflavor(request, dishDto);
        return R.success("新增菜品成功");
    }





    @GetMapping("/{id}")
    public R findId(@PathVariable Long id) {
        //检查参数
        if (id == null) {
            return R.error("参数异常!");
        }
        //调用dishService接口方法回显数据
        DishDto dishDto = dishService.findId(id);
        //判断如果查询到的对象为空
        if (dishDto == null) {
            return R.error("回显失败!该ID对象不存!");
        }
        return R.success(dishDto);
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto,HttpServletRequest request) {
        //检查参数
        if (dishDto == null) {
            return R.error("参数异常!");
        }
        //调用service接口方法修改数据
        boolean flag = dishService.updateByDish(dishDto,request);
        if (flag == false) {
            return R.error("修改失败,请重试!");
        }
        return R.success("修改成功!");
    }


    @DeleteMapping
    public R deleted(Long[] ids) {
        //检查参数
        if (ids.length <= 0) {
            return null;
        }
        boolean flag = dishService.deleted(ids);

        if (flag == false) {
            return R.error("删除失败!");
        }
        return R.success("删除成功!");
    }


    @PostMapping("/status/{status}")
    public R stopStatus(@PathVariable Integer status, Long[] ids) {
        //检查参数
        if (ids == null || (ids.length <= 0) || status == null) {
            return null;  //参数异常
        }
        boolean flag = dishService.updateByIds(status, ids);
        if (flag) {
            return R.success("已经批量停售!");
        }
        return R.error("批量停售失败!");
    }



}
